-
Notifications
You must be signed in to change notification settings - Fork 190
[WIP] mHC: Manifold-constrained Hyper Connection #1859
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
… kernel (#1877) * Refactor mHC kernel and wrapper to implement equations 14-18 with fused kernel * improve comments * Enhance documentation for mhc function: clarify equations, input/output shapes, and activation details * Enhance documentation in test_mhc.py: clarify equations, input/output shapes, and activation details for mHC kernel tests * Add _sinkhorn_knopp_log_domain_kernel to the fusion module * Add logging and sync Sinkhorn-Knopp function for doubly stochastic matrices * sync log-domain Sinkhorn-Knopp kernel for doubly stochastic matrix projection * Improve logging in mhc function to include all alpha parameters
aiter/ops/triton/fusions/mhc.py
Outdated
| else: | ||
| assert out.shape == (M, N), f"Output shape mismatch: expected ({M}, {N}), got {out.shape}" | ||
| assert out.dtype == x.dtype, f"Output dtype mismatch: expected {x.dtype}, got {out.dtype}" | ||
| assert out.device == x.device, f"Output device mismatch" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
…plified comments for sinkhorn-knopp impl
|
|
||
| # Res-stream: no constraints (identity activation) | ||
| # Just verify it exists | ||
| assert out_res.shape == (M, n_squared), f"Res-stream shape mismatch" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| # SPDX-License-Identifier: MIT | ||
| # Copyright (C) 2024-2025, Advanced Micro Devices, Inc. All rights reserved. | ||
|
|
||
| from .mhc_ref import * |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| # Copyright (C) 2024-2025, Advanced Micro Devices, Inc. All rights reserved. | ||
|
|
||
| from .mhc_ref import * | ||
| from .mla_decode_ref import * |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
|
|
||
| from .mhc_ref import * | ||
| from .mla_decode_ref import * | ||
| from .mla_extend_ref import * |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| from .mhc_ref import * | ||
| from .mla_decode_ref import * | ||
| from .mla_extend_ref import * | ||
| from .rotary_embedding import * |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| - H^res: [2n:2n+n²] residual connection (identity) (n² elements) | ||
| """ | ||
| x_f32 = x.to(torch.float32) | ||
| nC = x.shape[1] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| H_tilde = x_norm @ phi_f32 | ||
|
|
||
| # Split into three streams | ||
| n_squared = n * n |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* Refactor mHC kernel and wrapper to implement equations 14-18 with fused kernel * improve comments * Enhance documentation for mhc function: clarify equations, input/output shapes, and activation details * Enhance documentation in test_mhc.py: clarify equations, input/output shapes, and activation details for mHC kernel tests * Add _sinkhorn_knopp_log_domain_kernel to the fusion module * Add logging and sync Sinkhorn-Knopp function for doubly stochastic matrices * sync log-domain Sinkhorn-Knopp kernel for doubly stochastic matrix projection * Improve logging in mhc function to include all alpha parameters * Fix H dimensions * Refactor mHC function to return separate output tensors for pre, post, and residual streams * Refactor mhc_torch to return separate output tensors for pre, post, and residual streams * Adjust tolerance for is_doubly_stochastic assertion in test_sk_matrix_sizes for bfloat16 precision
…ke H_res doubly stochastic
anhminhnguyenhoang
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good, I would personally clean up the comments as they look a bit redundant
| H_res_torch.to(torch.float32), | ||
| atol=1e-2, | ||
| rtol=1e-2, | ||
| atol=5e-2, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did you run into test failure because of this for similar tests that you need to relax the tolerance?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, mainly because of sinkhorn which is an iterative process and returns higher differences due you only 10 iterations. May be we can try 20 for better results?
…o pre, post, and residual streams; update tests accordingly.
…ccuracy of assertions.
- Update benchmark script to use dynamic configurations
…lculations for clarity
…ity in stream handling
…parallelize computations along nC dimension with tests included
…Note that It is not optimized yet.
- 2D grid parallelization: (M/BLOCK_M, n²/BLOCK_SIZE) avoiding sequential M-row loop, processing BLOCK_M batches simultaneously per thread block - Vectorized softmax over n! permutations for BLOCK_M batches - Outer product accumulation for weighted sum computation - Config optimization: BLOCK_M=8, BLOCK_SIZE=16 (gfx942/gfx950)
…ve sinkhorn matching the paper's method. - Uses W^res ∈ ℝ^(nC×n!) instead of ℝ^(nC×n²) - Computes softmax over n! logits (Equation 4) - Constructs H^res as weighted sum of all n! permutation matrices (Equation 5) - Results in doubly stochastic matrix by construction (no Sinkhorn iterations needed)
Co-authors: @waqahmed-amd-fi @anhminhnguyenhoang
Motivation
Technical Details
Test Plan
Test Result
Submission Checklist